package src.edu.wayne.cs.csc4110.hw6.implementations.stateMachine;

import src.edu.wayne.cs.csc4110.hw6.implementations.userPanels.PanelFactoryImpl;
import src.edu.wayne.cs.csc4110.hw6.interfaces.User;


public class LoggedInState extends AbstractState {
	
	public LoggedInState(StateMachine stateMachine)
	{
		super(stateMachine);
	}

	@Override
	public void goToLoggedInState() {
		PanelFactoryImpl.getUserHomePanel(stateMachine);
	}

	@Override
	public void logOutCurrentUser(){
		stateMachine.getFrameWork().removeUserFromSession(stateMachine.getCurrentUser());
		if (stateMachine.getFrameWork().getUsersInSession().isEmpty())
		{
			stateMachine.setCurrentUser(null);
			stateMachine.setState(stateMachine.getNotLoggedInState());
			stateMachine.goToNotLoggedInState();
		}
		else
		{
			boolean flag = false;
			for(User user: stateMachine.getUsersInSession())
			{
				if(user != null){
					stateMachine.setCurrentUser(user);
					flag = true;
					break;
				}
			}
			
			if (flag)
			{
				stateMachine.goToLoggedInState();
			}
			else
			{
				stateMachine.setCurrentUser(null);
				stateMachine.setState(stateMachine.getNotLoggedInState());
				stateMachine.goToNotLoggedInState();
			}
		}
	}
}
